<?php
/**
 * Created by PhpStorm.
 * @description:
 * @time:
 * @Author: yfl
 * @QQ 554665488
 * Date: 2018-5-26
 * Time: 20:06
 */
//一个异步客户端 只可以在cli命令模式中运行
$async_client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);

//注册监听连接事件
$async_client->on('connect', function (Swoole\Client $client) {
    if ($client->errCode == 0) {
        echo '链接成功' . PHP_EOL;
    } else {
        echo '链接失败';
    }

    $client->send('异步链接成功后发送数据' . PHP_EOL);
});
$async_client->on('receive', function (Swoole\Client $client, $data) {
    echo '接收服务器发送的数据' . $data;

});
$async_client->on('error', function (Swoole\Client $client) {
});


//监听连接关闭事件,客服端关闭，或者服务器主动关闭
$async_client->on('close', function (Swoole\Client $client) {
    echo '客户端关闭链接';
});
$async_client->connect('127.0.0.1', 9501) || die('异步连接TCP服务器失败错误码' . $async_client->errCode . PHP_EOL);
